iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 14
0
自我挑戰組

軟體開發隨筆雜記--試著解決問題系列 第 14

[Python][OpenCV]如何利用haarcascade模型做辨識

  • 分享至 

  • xImage
  •  

Haar的cascade分類器: 在您下載了Opencv、complier並安裝之後 ,Opencv目錄下發現有個data folder ,下面的每個資料夾有些xml檔案,其屬於Opencv內建的Cascade Object detection資源檔,可用來偵測face、eyes、silverware、lowerbody、upperbody、fullbody、smile…等,我們可以很方便的直接使用。
https://ithelp.ithome.com.tw/upload/images/20200929/20119608sYejwcWHcL.jpg
以下是我做的介面
https://ithelp.ithome.com.tw/upload/images/20200929/20119608Abmzy7KIFd.jpg

先定義好我使用的工具模組,self.haarPanel使我使用的模板,

class OpenCVhaar():
    def __init__(self, master):
        self.parent = master
        self.imageFile = str()
        self.color_1 = (0,0,0)
        self.haarPanel = tk.LabelFrame(self.parent,
                                       text="haarcascade",
                                       font=('Courier', 10))
        self.haarPanel.pack(side=tk.LEFT, expand=tk.NO, fill = tk.X) 

        self.init_OpenCV_haar()
        self.init_haar_tab()
        self.init_setting_tab()

    def init_OpenCV_haar(self):
        self.OpenCV_haar_tab = tk.Frame(self.haarPanel)
        self.OpenCV_haar_tab.pack(side = tk.TOP, expand=tk.YES, fill=tk.BOTH)
        self.showhaarButton = tk.Button(self.OpenCV_haar_tab,
                                        text = "show haar",
                                        font=('Courier',10),
                                        command = self.show_haar)
        self.showhaarButton.pack(side=tk.TOP, expand=tk.YES, fill = tk.BOTH) 
    def show_haar(self, event = None):
        imageFile = self.imageFile
        #Call OpenCV Object img
        img = cv2.imread(imageFile)
        imOut = img
        #img of PIL object
        img_PIL = Image.open(imageFile)
        width, height = img_PIL.size
        imOut = self.haar_Detect(img)
        while True:
            # 顯示結果
            cv2.imshow("Output", imOut)
            #讀取使用者所按下的鍵
            k = cv2.waitKey(0) & 0xFF
            #若按下 q 鍵,則離開
            if k == 113:
                break
        #關閉圖形顯示視窗
        cv2.destroyAllWindows()
    def List_haar(self, event = None):
        haarlist = glob.glob( "haar/*.[xX][mM][lL]" )
        for xml in haarlist:
            (head, filename) = os.path.split(xml)
            self.Table_of_haar.insert("", 
                                      index = 'end',
                                      text = filename,
                                      values = (xml))

    def Select_haar(self, event=None):
        for item in self.Table_of_haar.selection():
            self.item_text = self.Table_of_haar.item(item, "values")
        self.haarcascade = cv2.CascadeClassifier(self.item_text[0])
        tkmsg.showinfo("Information",self.item_text[0])
    def haar_Detect(self, img):
        imOut = img
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        self.HaarDetect= self.haarcascade.detectMultiScale(gray, 1.3, 5)
        for (x,y,w,h) in self.HaarDetect:
            imgOut = cv2.rectangle(img,
            (x,y),
            (x+w,y+h),
            self.color_1,int(self.linesizespinbox.get()), self.fontlinetypecv2Var.get())
        return imOut

列出haar的xml檔的選單

    def init_haar_tab(self):
        self.haar_tab = tk.Frame(self.haarPanel)
        self.haar_tab.pack(side = tk.TOP, expand=tk.YES, fill=tk.BOTH)
        Listhaar = tk.Label(self.haar_tab)
        Listhaar.pack(side=tk.LEFT, expand=tk.NO)
        self.Table_of_haar = ttk.Treeview(Listhaar,height = 5)#,columns = ["#1"])
        self.Table_of_haar.heading("#0", text = "List of haar")#icon column
        self.Table_of_haar.column("#0", width = 500)#icon column
        self.Table_of_haar.tag_configure('T', font = 'Courier,4')
        self.Table_of_haar.bind("<Double-1>",self.Select_haar)
        self.Table_of_haar.pack(side=tk.TOP, expand=tk.YES)
        self.List_haar_Button = tk.Button(Listhaar,
                                          text = "List haar",
                                          font=('Courier', 10),
                                          width = 15,
                                          command = self.List_haar)
        self.List_haar_Button.pack(side=tk.TOP, expand=tk.YES, fill = tk.BOTH)
    def init_setting_tab(self):
        self.setting_tab = tk.Frame(self.haarPanel)
        self.setting_tab.pack(side = tk.TOP, expand=tk.YES, fill=tk.BOTH)
        self.MarkSettingPanel = tk.LabelFrame(self.setting_tab,
                                              text="Setting Panel",
                                              font=('Courier', 10))
        self.MarkSettingPanel.pack(side=tk.TOP, expand=tk.YES, fill=tk.BOTH)
        '''Color Panel'''
        ColorPanel = tk.Frame(self.MarkSettingPanel)
        ColorPanel.grid(row = 0, column = 0 ,sticky = tk.E+tk.W)
        self.Color1Button = tk.Button(ColorPanel,
                                      text = "Color 1",
                                      font=('Courier', 10),
                                      command = self.askcolor1)
        self.Color1Button.grid(row = 0, column = 0, sticky = tk.E+tk.W)
        '''font line type setting'''
        fontcv2Panel = tk.Frame(self.MarkSettingPanel)
        fontcv2Panel.grid(row = 0, column = 1 ,sticky = tk.E+tk.W)

        '''Line Size'''
        tk.Label(self.MarkSettingPanel,
                 text = "Line size",
                 font=('Courier', 10)).grid(row = 0,
                                            column = 4,
                                            sticky = tk.E+tk.W)        

        self.linesizespinbox = tk.Spinbox(self.MarkSettingPanel,
                                          values = linewidth,
                                          width = 3)
        self.linesizespinbox.grid(row = 0, column = 5, sticky = tk.E+tk.W)
        '''font line type setting'''
        fontlinetypecv2Panel = tk.Frame(self.MarkSettingPanel)
        fontlinetypecv2Panel.grid(row = 0, column = 6 ,sticky = tk.E+tk.W)
        
        '''line type label'''
        tk.Label(fontlinetypecv2Panel,
                 text = "line type",
                 font=('Courier', 10)).pack(side = tk.TOP,
                                            expand=tk.YES,
                                            fill=tk.BOTH)
        self.fontlinetypecv2Var = tk.IntVar()
        self.fontlinetypecv2Var.set(8)
        for val, linetype, in fontlinetype_Item.items(): 
            tk.Radiobutton(fontlinetypecv2Panel,
                           text = linetype,
                           variable = self.fontlinetypecv2Var,
                           value = val,
                           font=('Courier', 10)).pack(side = tk.LEFT,
                                                      expand=tk.YES,
                                                      fill=tk.BOTH)
  def askcolor1(self, event = None):
               .
               .
               .
               

上一篇
如何使用OpenALPR
下一篇
[Python][OpenCV]Selective Search
系列文
軟體開發隨筆雜記--試著解決問題33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言